package com.fau.cm.action.index;

import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalTariff;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 资费表管理Action
 * @author liy
 * @datetime 2014年11月19日,下午4:28:58
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class CallTariffAction extends PublicAction implements ModelDriven<MedicalTariff>{
	
	private static final long serialVersionUID = -2203454771609543218L;
	
	private MedicalTariff tariff = new MedicalTariff();

	/**
	 * @desc 跳转到资费表管理页面
	 * @return SUCCESS
	 * @author liy
	 * @datetime 2014年11月19日,下午4:35:42
	 */
	@Action(value = "call_tariff_init",results = {@Result(location = "call_tariff_init.jsp")})
	public String init() {
		return SUCCESS;
	}
	
	/**
	 * @desc 进入资费表管理添加页面
	 * @return String
	 * @author liy
	 * @datetime 2014年12月10日,下午8:14:49
	 */
	@Action(value = "call_tariff_add",results = {@Result(location = "call_tariff_add.jsp")})
	public String add() {
		return SUCCESS;
	}
	
	/**
	 * @desc 进入资费表管理修改页面
	 * @return String
	 * @author liy
	 * @datetime 2014年12月11日,上午12:26:53
	 */
	@Action(value = "call_tariff_edit",results = {@Result(location = "call_tariff_edit.jsp")})
	public String edit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 分页查询资费管理表
	 * @return null
	 * @author liy
	 * @datetime 2014年12月10日,下午5:17:56
	 */
	@Action(value = "call_tariff_list")
	public String page(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ServletActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		pageBean = this.baseAllService.getBillingManagerService().findTariffByPage(map, this.tariff, admin.getEnterpriseId());
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 保存资费管理
	 * @return null
	 * @author liy
	 * @datetime 2014年12月10日,下午9:54:01
	 */
	@Action(value = "call_tariff_save")
	public String save(){
		this.baseAllService.getBillingManagerService().insertTariff(this.tariff);
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		this.map.put("success", true);
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 更新资费管理
	 * @return null
	 * @author liy
	 * @datetime 2014年12月11日,上午12:25:15
	 */
	@Action(value = "call_tariff_update")
	public String update(){
		this.baseAllService.getBillingManagerService().insertTariff(this.tariff);
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		this.map.put("success", true);
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 删除资费管理
	 * @return null
	 * @author liy
	 * @datetime 2014年12月10日,下午10:22:15
	 */
	@Action(value = "call_tariff_del")
	public String delete(){
		this.baseAllService.getBillingManagerService().deleteTariff(this.tariff);
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		this.map.put("success", true);
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 查询中继表
	 * @return null
	 * @author liy
	 * @datetime 2014年12月10日,下午11:19:52
	 */
	@Action(value = "call_relay_list")
	public String relayList(){
		this.list = this.baseAllService.getBillingManagerService().findRelay();
		HttpServletResponse response = ServletActionContext.getResponse();
		formatJSON(response, this.list);
		return null;
	}

	@Override
	public MedicalTariff getModel() {
		return tariff;
	}
}
